﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLVMBAYDatacess;
using QLVMBAYDomain;
using System.Data;

namespace QLVMBAY.Admin
{
    public partial class QLQuocGia : System.Web.UI.Page
    {
        static bool Insert =false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadGrid();
            }
        }
        private void LoadGrid()
        {
            grdQuocGia.DataSource = QuocGiaDA.Inst.GetAll(false);
            grdQuocGia.DataBind();
            if (grdQuocGia.PageCount <= 1)
            {
                grdQuocGia.PagerStyle.Visible = false;
            }
        }

        protected void AddButton_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = true;            
            pnView.Visible = false;
            txtMaQG.Text = "";
            txtTenQG.Text = "";
            Insert = true;
        }

        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            LoadGrid();
        }

        protected void Update_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                QuocGia QG = new QuocGia();
                QG.MaQG = txtMaQG.Text.ToString();
                QG.TenQG = txtTenQG.Text.ToString();
                QG.IsMacDinh  = 0;
                if (Insert == true)
                {
                    QuocGiaDA.Inst.Insert(QG);
                }
                else
                {
                    QuocGiaDA.Inst.Update(QG);
                }
                LoadGrid();
                pnView.Visible = true;
                pnUpdate.Visible = false;
                Insert = false;
            }
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            pnView.Visible = true;
            pnUpdate.Visible = false;
            Insert = false;
        }

        protected void grdQuocGia_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            var itemQG = QuocGiaDA.Inst.GetByPrimaryKey(false, e.Item.Cells[0].Text);
            switch (e.CommandName)
            {
                case "Edit":
                    Insert = false;
                    txtMaQG.Text = itemQG.MaQG.ToString();
                    txtMaQG.ReadOnly = true;
                    txtTenQG.Text = itemQG.TenQG.ToString();
                    pnView.Visible = false;
                    pnUpdate.Visible = true;
                    break;

                case "Delete":
                    QuocGiaDA.Inst.Delete(itemQG);
                    LoadGrid();
                    break;
            }
        }

        protected void grdQuocGia_ItemDataBound(object sender, DataGridItemEventArgs e)
        {

        }

        protected void grdQuocGia_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdQuocGia.CurrentPageIndex = e.NewPageIndex;
            LoadGrid();
        }
    }
}